<template>
	<view class="content-box">
		<u-navbar
      :title="$t('user.Privacy-and-Security')"
      :autoBack="false"
			@leftClick="gotoBack"
      bgColor="#F6F6F6"
      titleStyle="font-weight: 500; color: #000000;"
      :placeholder="true"
    >
    </u-navbar>
		<view class="content">
			<view class="box">
				<!-- 阻止用户 -->
				<view class="line-item" @click="toPage()">
					<view class="item-left">{{$t('setting.BlockUser')}}</view>
					<view class="item-right">
						<text>{{bolackTotal?bolackTotal:$t('user.None')}}</text>
						<text class="iconfont icon-jiantou-you"></text>
					</view>
				</view>
			</view>
			<view class="title">{{ $t('user.Privacy') }}</view>
			<view class="box">
				<!-- 电话号码 -->
				<view class="line-item" @click="showDialog(1)">
					<view class="item-left">{{$t('user.Phone-Number')}}</view>
					<view class="item-right">
						<text>{{getItemText(userInfo.userPhonePrivacy)}}</text>
						<text class="iconfont icon-jiantou-you"></text>
					</view>
				</view>
				<!-- 邮箱 -->
				<view class="line-item" @click="showDialog(2)">
					<view class="item-left">{{$t('user.Email')}}</view>
					<view class="item-right">
						<text>{{getItemText(userInfo.emailPrivacy)}}</text>
						<text class="iconfont icon-jiantou-you"></text>
					</view>
				</view>
				<!-- 编码 -->
				<view class="line-item" @click="showDialog(3)">
					<view class="item-left">{{ $t('user.Encoding') }}</view>
					<view class="item-right">
						<text>{{getItemText(userInfo.encodingPrivacy)}}</text>
						<text class="iconfont icon-jiantou-you"></text>
					</view>
				</view>
			</view>
		</view>

		<!-- 设置是否可见弹框 -->
		<SelectPopup :title="$t('user.Vsible-Range')" :show.sync="show" :option="opetation" :value="privacyValue"
			@finish="onChangePrivacy" v-if="show">
		</SelectPopup>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from "vuex";
	import {
		queryUserInfo,
		updateUserInfo
	} from "@/services/user";
	import SelectPopup from "@/components/SelectPopup.vue";
	import { blockUserList} from '@/services/user'
	export default {
		data() {
			return {
				show: false,
				settingChoose: 0,
				bolackTotal:0,
				opetation: [{
						// 所有人可见
						label: this.$t('user.Everybody'),
						value: 2
					},
					{
						// 列表可见
						label: this.$t('user.My-contacts'),
						value: 1
					},
					{
						// 都不可见
						label: this.$t('user.Nobody'),
						value: 0
					},
				],
				privacyValue: 0, // 隐式值
				popupType: 1 // 区分选中的是电话 或邮箱 或编码
			};
		},
		components: {
			SelectPopup
		},
		computed: {
			...mapState(['userInfo']),
		},
		onShow() {
			this.getBlackList()
			uni.setNavigationBarTitle({
				title: this.$t('user.Privacy-and-Security')
			})
		},
		methods: {
			...mapActions(["changeUserInfo"]),
			// 返回上一页
			gotoBack() {
				uni.switchTab({
					url: '/pages/user/User'
				});
			},
			// 跳转设置阻止指定用户
			toPage() {
				uni.navigateTo({
					url: "/pagesUser/privacy/Blocked",
				});
			},
			// 分页获取个人黑名单列表
      async getBlackList () {
				const resp =  await blockUserList()
				if(!resp.success)return
				this.bolackTotal = resp.result.total
			},
			// 切换可见状态
			changeChoose(index) {
				this.settingChoose = index;
			},
      // 打开弹框 val 
      //  1:电话号码
      //  2:邮箱
      //  3:编码
			showDialog(val) {
				switch (val) {
					case 1: // 电话号码
						this.popupType = 1
						this.privacyValue = this.userInfo.userPhonePrivacy
						break;
					case 2: // 邮箱
						this.popupType = 2
						this.privacyValue = this.userInfo.emailPrivacy
						break;
					case 3: // 编码
						this.popupType = 3
						this.privacyValue = this.userInfo.encodingPrivacy
						break;
					default:
						break;
				}
				this.show = true;
			},
			onChangePrivacy(value) {
				switch (this.popupType) {
					case 1:
						this._updateUserInfo('userPhonePrivacy', value)
						break;
					case 2:
						this._updateUserInfo('emailPrivacy', value)
						break;
					case 3:
						this._updateUserInfo('encodingPrivacy', value)
						break;
					default:
						break;
				}
			},
			// 修改隐私选项
			async _updateUserInfo(label, value) {
				const { success } = await updateUserInfo({
						[label]: value
					})
				if (success) {
					this.handleUserInfo()
				}
			},
			//获取用户信息
			async handleUserInfo() {

				const resp = await queryUserInfo(); // 查询用户信息
				if (!resp.success) return;
				// this.userInfo = resp.result.data;
				this.changeUserInfo(resp.result.data); // 更新用户信息
			},
			getItemText(val) {
				switch (val) {
					case 0:
						return this.$t('user.Nobody') // 不可见
						break;
					case 1:
						return this.$t('user.My-contacts') // 联系人可见
						break;
					case 2:
						return this.$t('user.Everybody') // 所有人可见
						break;
					default:
						return ''
						break;
				}
			},
			// 确定切换可见状态
			confirmChoose() {}
		},
	};
</script>

<style lang="scss" scoped>
	.content-box {
		width: 100%;
		box-sizing: border-box;
		min-height: 100vh;
		background: #f6f6f6;

		.content {
			width: 100%;
			box-sizing: border-box;
			padding: 67rpx 30rpx 0;
		}

		.box {
			width: 100%;
			box-sizing: border-box;
			border-radius: 20rpx;
			background: #fff;
			padding: 0 33rpx;
		}

		.title {
			font-size: 24rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #878787;
			margin: 40rpx 0 18rpx;
			margin-left: 31upx;

		}

		.line-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 112rpx;
			background: #ffffff;
			border-bottom: 2rpx solid #f6f6f6;

			.item-left {
				font-size: 30rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #000000;
			}

			.item-right {
				display: flex;
				align-items: center;
				font-size: 30rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #878787;

				.icon-jiantou-you {
					margin-left: 30rpx;
				}
			}
		}

		.line-item :nth-last-child(1) {
			border-bottom: none;
		}
	}

	// 是否可见弹框
	.overlay-warp {
		width: 100%;
		position: fixed;
		left: 0;
		right: 0;
		border-radius: 50rpx 50rpx 0px 0px;
		bottom: 0;

		.rect {

			// border-radius: 50rpx 50rpx 0px 0px;
			.menu-top {
				background: #f6f6f6;
				border-radius: 50rpx 50rpx 0px 0px;
				padding: 0 30rpx;
				height: 111rpx;
				line-height: 111rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 28rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;

				.cancel {
					flex: 1;
					color: #878787;
					text-align: left;
				}

				.menu-title {
					flex: 1;
					font-size: 34rpx;
					font-family: Source Han Sans CN;
					font-weight: 500;
					color: #000000;
				}

				.confirm {
					text-align: right;
					flex: 1;
					color: #61bdb2;
				}
			}

			.menu-list {
				background: #fff;
				padding: 0 33rpx 50rpx;
				padding: 0 33rpx 50rpx;

				.choose-item {
					height: 112rpx;
					line-height: 112rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 2rpx solid #f6f6f6;

					.label {
						font-size: 30rpx;
						font-family: Source Han Sans CN;
						font-weight: 400;
						color: #000000;
					}

					.icon-duigou {
						color: #61bdb2;
						font-size: 20px;
					}
				}
			}
		}
	}
</style>
